package initdb

import "gitee.com/zaiqiang231/go-base-app/base_app/store"

type DbTable[T store.TableInterface] struct {
	Table T
}

func (i DbTable[T]) GetTable() store.TableInterface {
	return i.Table
}

func (i DbTable[T]) InitializerName(table store.SubInitializer) string {
	return table.GetTable().TableName()
}

func (i DbTable[T]) HasTable(ds *store.Datastore, table store.SubInitializer) bool {
	return ds.DB.Migrator().HasTable(table.GetTable())
}

func (i DbTable[T]) CreateTable(ds *store.Datastore, table store.SubInitializer) error {
	return ds.DB.Set("gorm:table_options", "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4").
		Migrator().AutoMigrate(table.GetTable())
}

func (i DbTable[T]) UpdateTable(ds *store.Datastore, table store.SubInitializer) error {
	return ds.DB.Migrator().AutoMigrate(table.GetTable())
}

func (i DbTable[T]) InitData(ds *store.Datastore) {
}
